<?php

/**
 * 后台文章管理器
 */
class ArticleController extends PlatformController {
	/**
	 * 文章首页动作
	 */
	public function indexAction() {
		//提取所有文章的信息
		$article = Factory::M('ArticleModel');
		$artInfo = $article->getArticle();
		//分配变量
		$this->assign('artInfo',$artInfo);
		//输出视图
		$this->display('index.html');
	}
	/**
	 * 展示添加文章的表单
	 */
	public function addAction() {
		//提取所有的分类的信息
		$category = Factory::M('CategoryModel');
		$cateInfo = $category->getCategory();
		//分配变量
		$this->assign('cateInfo',$cateInfo);
		$this->display('add.html');
	}
	/**
	 * 添加文章动作
	 */
	public function dealAddAction() {
		//接收表单
		$art = array();
		$art['cate_id'] = $_POST['cate_id'];
		$art['title'] = $this->escapeData($_POST['title']);
		$art['author'] = $this->escapeData($_POST['author']);
		$art['art_desc'] = $this->escapeData($_POST['art_desc']);
		$art['content'] = $this->addslashes($_POST['content']);
		//判断数据的合法性
		if(empty($art['title']) || empty($art['author']) || empty($art['art_desc']) || empty($art['content'])) {
			$this->jump('index.php?p=Back&c=Article&a=Add',':( 填写的信息不完整！');
		}
		//判断是否有缩略图上次，暂时省略
		if($_FILES['thumb']['error'] != 4) {
			//说明有文件上传
			$upload = new Upload;//实例化上传类
			$allow = array('image/png','image/jpeg','image/gif','image/jpg');
			$path = UPLOADS_DIR . 'thumb/' . date('Ymd');

			if(!file_exists($path)) {
				mkdir($path);
			}

			//调用上传类的核心方法
			$result = $upload->uploadAction($_FILES['thumb'],$allow,$path);
			if($result) {
				//上传成功
				$art['thumb'] = $result;
			}else {
				//上传失败
				$error = Upload::$error;
				$this->jump('index.php?p=Back&c=Article&a=add',$error);
			}
		}else {
			//说明没有上传缩略图
			$art['thumb'] = 'default.jpg';
		}
		//数据入库，调用模型
		$article = Factory::M('ArticleModel');
		$result = $article->insertArt($art);
		if($result) {
			$this->jump('index.php?p=Back&c=Article&a=index',"文章标题为：{$art['title']},添加成功！");
		}else {
			$this->jump('index.php?p=Back&c=Article&a=add',':( 发生未知错误，发布文章失败');
		}
	}
}
